package org.easyelf.pitaya.admin.sys.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.kisso.SSOHelper;
import com.baomidou.kisso.SSOToken;
import com.baomidou.kisso.common.encrypt.SaltEncoder;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.easyelf.pitaya.admin.sys.entity.SysUser;
import org.easyelf.pitaya.admin.sys.service.ISysUserService;
import org.easyelf.springmvc.core.base.BaseController;
import org.easyelf.springmvc.plugins.easyui.DataGrid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;


/**
 * @Title: 用户
 * @Description: 前端控制器
 * @Package: ${org.easyelf.pitaya.admin.sys.entity} with IntelliJ IDEA
 * @User: Rehn.gaO
 * @Date: 2017-03-03
 * @Time: ${time}
 */
@Controller
@RequestMapping("/sysUser")
public class SysUserController extends BaseController {

	@Autowired
	private ISysUserService sysUserService;

	@PostMapping(value = "/index")
	public String index() {
		return "admin/sys/user/index.html";
	}

	@RequestMapping(value = "/page")
	@ResponseBody
	public JSONObject page(DataGrid<SysUser> dataGrid) {
		//第一种查询方式：采用MybatisPageHelper
		PageHelper.startPage(dataGrid.getPage(), dataGrid.getRows());
		List<SysUser> sysUserList = sysUserService.selectList(new EntityWrapper<SysUser>().orderBy(dataGrid.getSort(),dataGrid.isAsc()));
		//第二种查询方式：Mybatis-plus
//		sysUserService.selectPage(this.<SysUser>getPage());
		return renderPage(sysUserList);
	}

	@RequestMapping("/preSave")
	public ModelAndView preSave(ModelAndView modelAndView, @RequestParam(value = "id", required = false) Long id) {
		modelAndView.setViewName("admin/sys/user/user.html");
		if (id != null) {
			modelAndView.addObject("sysUser", sysUserService.selectById(id));
		}
		return modelAndView;
	}

	@ResponseBody
	@RequestMapping("/save")
	public Object save(SysUser sysUser) {
		//新增操作
		if (sysUser.getId() == null) {
			SysUser existUser = sysUserService.selectByLoginName(sysUser.getUsername());
			if (existUser == null) {
            	/* 演示不验证表单，用户名作为密码盐值 */
				sysUser.setPassword(SaltEncoder.md5SaltEncode(sysUser.getUsername(), sysUser.getPassword()));
				sysUser.setGmtCreate(new Date());
				return sysUserService.insert(sysUser) ? renderSuccess("添加成功") : renderError("添加失败");
			} else {
				renderError("注册用户名【" + sysUser.getUsername() + "】已存在！");
			}
		} else {
			//修改操作
            sysUser.setPassword(SaltEncoder.md5SaltEncode(sysUser.getUsername(), sysUser.getPassword()));
			sysUser.setGmtModified(new Date());
			return sysUserService.updateById(sysUser) ? renderSuccess("修改成功") : renderError("修改失败");
		}
		return renderError("操作异常");
	}

	@ResponseBody
	@RequestMapping("/delete")
	public Object delete(@RequestParam(value = "id", required = false) Long id) {
		return sysUserService.deleteById(id) ? renderSuccess("删除成功") : renderError("删除失败");
	}
}